﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Project3
{
    class RandomObject
    {
        PhibesGame game;
        Model model;
        Vector3 position = Vector3.Zero;
        Vector3 p0, p1, p2, p3;
        Vector3 v0, v1, v2, v3;
        int time; 
        bool flag = true;

        public RandomObject(PhibesGame game)
        {
            this.game = game;
            p0 = new Vector3(-1000f, 0f, 500f);
            p1 = new Vector3(100f, 500f, 30f);
            p2 = new Vector3(150f, 140f, -37f);
            p3 = new Vector3(1205f, 1000f, -400f);

            v0 = new Vector3(1205f, 1000f, -400f);
            v1 = new Vector3(-10f, 14f, -50f);
            v2 = new Vector3(-16f, 51f, -18f);
            v3 = new Vector3(-1000f, 0f, 500f);
        }

        public void LoadContent(ContentManager content)
        {
            model = content.Load<Model>("Asteroid" + (game.Rand(4) + 1));
        }

        public void Update(GameTime gameTime)
        {
            //Move this object along a curve
            
            Vector3 tmp;
            float count = 100;
            if (flag)
            {
                time += 1;
                tmp = GetPoint((time/count),p0, p1, p2, p3);
                if (time > 100)
                {
                    flag = false;
                    time = 0;
                }
            }
            else
            {
                tmp = GetPoint((time/count),v0, v1, v2, v3);
                time += 1;
                if (time > 100)
                {
                    flag = true;
                    time = 0;
                }
            }
            
            position.X = tmp.X;
            position.Y = tmp.Y;
            position.Z = tmp.Z;
        }

        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            Matrix transform = Matrix.CreateTranslation(position);
            DrawModel(graphics, model, transform);
        }

        private void DrawModel(GraphicsDeviceManager graphics, Model model, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * world;
                    effect.View = game.Camera.View;
                    effect.Projection = game.Camera.Projection;
                }
                mesh.Draw();
            }
        }

        private Vector2 GetPoint(float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
        {
            float cx = 3 * (p1.X - p0.X);
            float cy = 3 * (p1.Y - p0.Y);

            float bx = 3 * (p2.X - p1.X) - cx;
            float by = 3 * (p2.Y - p1.Y) - cy;

            float ax = p3.X - p0.X - cx - bx;
            float ay = p3.Y - p0.Y - cy - by;

            float Cube = t * t * t;
            float Square = t * t;

            float resX = (ax * Cube) + (bx * Square) + (cx * t) + p0.X;
            float resY = (ay * Cube) + (by * Square) + (cy * t) + p0.Y;

            return new Vector2(resX, resY);
        }

        private Vector3 GetPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
        {
            float cx = 3 * (p1.X - p0.X);
            float cy = 3 * (p1.Y - p0.Y);
            float cz = 3 * (p1.Z - p0.Z);

            float bx = 3 * (p2.X - p1.X) - cx;
            float by = 3 * (p2.Y - p1.Y) - cy;
            float bz = 3 * (p2.Z - p1.Z) - cz;

            float ax = p3.X - p0.X - cx - bx;
            float ay = p3.Y - p0.Y - cy - by;
            float az = p3.Z - p0.Z - cz - bz;

            float Cube = t * t * t;
            float Square = t * t;

            float resX = (ax * Cube) + (bx * Square) + (cx * t) + p0.X;
            float resY = (ay * Cube) + (by * Square) + (cy * t) + p0.Y;
            float resZ = (az * Cube) + (bz * Square) + (cz * t) + p0.Z;

            return new Vector3(resX, resY, resZ);
        }
    }
}
